<?php
namespace Admin\Controller;
use Think\Controller;
class ElectricianController extends CommonController {
    //电工列表
    public function electrician_list(){
        $map = '';

        $is_check = I('is_check');
        if($is_check  != ''){
            $map['check_status'] = $is_check;
        }

        $search_content = I('search_content');
        if($search_content  != ''){
            $where['real_name'] = array('like',"%$search_content%");
            $where['phone'] = array('like',"%$search_content%");
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }

        $row = 10;
        $p = $_GET['p'] ? $_GET['p'] : 1;
        $count = M('electrician')
            ->join('AS e JOIN __USER__ AS u ON e.electrician_id = u.id')
            ->join('JOIN __AREA__ pr ON pr.code = e.province')
            ->join('JOIN __AREA__ ci ON ci.code = e.city')
            ->join('JOIN __AREA__ co ON co.code = e.county')
            ->join('JOIN __AREA__ tw ON tw.code = e.town')
            ->where($map)
            ->count();
        $Page = new \Think\Page($count,$row);
        $show = $Page->show();
        $list = M('electrician as e')
            ->field('e.*,u.phone,pr.name as province_name,ci.name as city_name,co.name as county_name,tw.name as town_name')
            ->join('JOIN __AREA__ pr ON pr.code = e.province')
            ->join('JOIN __AREA__ ci ON ci.code = e.city')
            ->join('JOIN __AREA__ co ON co.code = e.county')
            ->join('JOIN __AREA__ tw ON tw.code = e.town')
            ->join('JOIN __USER__ AS u ON e.electrician_id = u.id')
            ->where($map)
            ->page($p,$row)
            ->select();
        $this->assign('p',$p);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->assign('is_check',$is_check);
        $this->assign('search_content',$search_content);
        $this->display();
    }

    //编辑电工
    public function electrician_edit(){
        $info = M('electrician')->where('id='.I('id'))->find();
        if(IS_POST){
            $id = I('id');
            //如果能接收到文件，执行上传
            if($_FILES){
                $picture = upload();
                if($picture['identity_card_front']) {
                    $data['identity_card_front'] = $picture['identity_card_front'];
                    unlink(ltrim($info['identity_card_front'],'/'));//删除已经上传的图片
                }
                if($picture['identity_card_reverse']) {
                    $data['identity_card_reverse'] = $picture['identity_card_reverse'];
                    unlink(ltrim($info['identity_card_reverse'],'/'));
                }
                if($picture['electrician_license_front']) {
                    $data['electrician_license_front'] = $picture['electrician_license_front'];
                    unlink(ltrim($info['electrician_license_front'],'/'));
                }
                if($picture['electrician_license_reverse']) {
                    $data['electrician_license_reverse'] = $picture['electrician_license_reverse'];
                    unlink(ltrim($info['electrician_license_reverse'],'/'));
                }
            }
            $data['real_name'] = I('real_name');
            $data['id_number'] = I('id_number');
            $data['check_status'] = I('check_status');
            if($info['check_status'] != $data['check_status']){
                $data['check_time'] = date('Y-m-d H:i:s');
            }
            if(false === M('electrician')->where('id='.$id)->save($data)){
                $this->error('修改失败');
            }else{
                //如果修改之前的审核状态不是已审核，现在修改为已审核，修改用户的类型（改为电工）和vip
                if($info['check_status'] != 1 && $data['check_status'] == 1){
                    $edit['user_type'] = 2;
                    $edit['vip'] = 1;
                    if(false === M('user')->where(array('id'=>$info['electrician_id']))->save($edit)){
                        $this->error('修改失败');
                    }
                }
                //如果修改之前的审核状态已审核，现在修改为待审核或者审核未通过，修改用户的类型（改为电工）和vip
                if($info['check_status'] == 1 && $data['check_status'] != 1){
                    $edit['user_type'] = 1;
                    $edit['vip'] = 0;
                    if(false === M('user')->where(array('id'=>$info['electrician_id']))->save($edit)){
                        $this->error('修改失败');
                    }
                }

                $this->success('修改成功',U('electrician_list',array('p'=>I('p'),'is_check'=>I('is_check'),'search_content'=>I('search_content'))));
            }
        }

        //获取所有省份
        $province_list = M('area')->where(array('parentId' => 0))->select();

        //获取所有的市
        $city_list = M('area')->where(array('parentId' => $info['province']))->select();

        //获取所有的县
        $county_list = M('area')->where(array('parentId' => $info['city']))->select();

        //获取所有的镇
        $town_list = M('area')->where(array('parentId' => $info['county']))->select();

        //获取筛选和分页参数，分配变量
        $this->assign('province_list',$province_list);
        $this->assign('city_list',$city_list);
        $this->assign('county_list',$county_list);
        $this->assign('town_list',$town_list);
        $this->assign('p',I('p'));
        $this->assign('is_check',I('is_check'));
        $this->assign('search_content',I('search_content'));
        $this->assign('info',$info);
        $this->display();
    }

    //电工服务协议条款
    public function electrician_agreement(){
        if(IS_POST){
            $editResult = M('electrician_agreement')->where(array('id'=>1))->save(array('electrician_agreement'=>I('electrician_agreement')));
            if($editResult === false){
                $this->error('保存失败');
            }else{
                $this->success('保存成功');
            }
            exit;
        }else{
            $electrician_agreement = M('electrician_agreement')->where(array('id'=>1))->getField('electrician_agreement');
            $this->assign('electrician_agreement',$electrician_agreement);
            $this->display();
        }
    }

    //删除
    public function electrician_del(){
        $electrician_id = I('electrician_id');
        $p = I('p');
        $is_check = I('is_check');
        $search_content = I('search_content');
        if(M('electrician')->where('electrician_id='.$electrician_id)->delete()){
            //还原用户表中的普通用户类型和非VIP状态
            if(false === M('user')->where('id='.$electrician_id)->save(array('user_type'=>1,'vip'=>0))){
                $this->error('数据异常');
            }
            //修改用户session信息
            session('user_vip',0);
            session('user_type',1);
            session('user_real_name',null);

            $this->success('删除成功',U('electrician_list',array('p'=>$p,'is_check'=>$is_check,'search_content'=>$search_content)));
        }else{
            $this->error('数据异常',U('electrician_list',array('p'=>$p,'is_check'=>$is_check,'search_content'=>$search_content)));
        }
    }

    //AJAX -- 根据所选省份，获取所有市
    public function city()
    {
        $city_list = M('area')->field('code,name')->where(array('parentId' => I('province')))->select();
        echo json_encode($city_list);
    }

    //AJAX -- 根据所选城市，获取所有县区
    public function county()
    {
        $county_list = M('area')->field('code,name')->where(array('parentId' => I('city')))->select();
        echo json_encode($county_list);
    }

    //AJAX -- 根据所选县区，获取所有乡镇
    public function town()
    {
        $county_list = M('area')->field('code,name')->where(array('parentId' => I('county')))->select();
        echo json_encode($county_list);
    }
}